/*----------- Tank -----------*/
var TANK_MOVE_UP = 0;
var TANK_MOVE_RIGHT = 1;
var TANK_MOVE_DOWN = 2;
var TANK_MOVE_LEFT = 3;
var TANK_STAND_UP = 4;
var TANK_STAND_RIGHT = 5;
var TANK_STAND_DOWN = 6;
var TANK_STAND_LEFT = 7;
var TANK_SHOOT_UP = 8;
var TANK_SHOOT_RIGHT = 9;
var TANK_SHOOT_DOWN = 10;
var TANK_SHOOT_LEFT = 11;
var TANK_EXPLODE = 12;

function Tank(x, y, name, collisionManager) {
    this.collisionManager = collisionManager;
    this.x = x;
    this.y = y;
    this.halfWidth = 15;
    this.halfHeight = 15;
    this.x1 = this.x - this.halfWidth;
    this.x2 = this.x + this.halfWidth;
    this.y1 = this.y - this.halfHeight
    this.y2 = this.y + this.halfHeight
    this.dx = 0;
    this.dy = 0;
    this.name = name;
    this.nameOffsetX = 16;
    this.nameOffsetY = 16;
    this.lifeOffsetX = 16;
    this.lifeOffsetY = -16;
    this.life = 5;
    this.projectiles = [];
    this.face = FACE_UP;
    this.notShooting = true;
    this.remove = false;
    this.visible = true;

    var up = {
        cellWidth: 32,
        cellHeight: 32,
        interval: 200,
        offsetX: 16,
        offsetY: 16,
        gridSpace: 1,
        frames: [{
            x: 16,
            y: 1
        }, {
            x: 17,
            y: 1
        }, {
            x: 18,
            y: 1
        }, {
            x: 19,
            y: 1
        }, {
            x: 20,
            y: 1
        }, {
            x: 21,
            y: 1
        }, {
            x: 22,
            y: 1
        }]
    };

    var right = {
        cellWidth: 32,
        cellHeight: 32,
        interval: 200,
        offsetX: 16,
        offsetY: 16,
        gridSpace: 1,
        frames: [{
            x: 16,
            y: 3
        }, {
            x: 16,
            y: 4
        }, {
            x: 16,
            y: 5
        }, {
            x: 16,
            y: 6
        }, {
            x: 16,
            y: 7
        }, {
            x: 16,
            y: 8
        }, {
            x: 16,
            y: 9
        }]
    };

    var down = {
        cellWidth: 32,
        cellHeight: 32,
        interval: 200,
        offsetX: 16,
        offsetY: 16,
        gridSpace: 1,
        frames: [{
            x: 16,
            y: 2
        }, {
            x: 17,
            y: 2
        }, {
            x: 18,
            y: 2
        }, {
            x: 19,
            y: 2
        }, {
            x: 20,
            y: 2
        }, {
            x: 21,
            y: 2
        }, {
            x: 22,
            y: 2
        }]
    };

    var left = {
        cellWidth: 32,
        cellHeight: 32,
        interval: 200,
        offsetX: 16,
        offsetY: 16,
        gridSpace: 1,
        frames: [{
            x: 17,
            y: 3
        }, {
            x: 17,
            y: 4
        }, {
            x: 17,
            y: 5
        }, {
            x: 17,
            y: 6
        }, {
            x: 17,
            y: 7
        }, {
            x: 17,
            y: 8
        }, {
            x: 17,
            y: 9
        }]
    };

    var standUp = {
        cellWidth: 32,
        cellHeight: 32,
        gridSpace: 1,
        x: 15,
        y: 1,
        offsetX: 16,
        offsetY: 16
    };

    var standRight = {
        cellWidth: 32,
        cellHeight: 32,
        gridSpace: 1,
        x: 20,
        y: 4,
        offsetX: 16,
        offsetY: 16
    };

    var standDown = {
        cellWidth: 32,
        cellHeight: 32,
        gridSpace: 1,
        x: 15,
        y: 6,
        offsetX: 16,
        offsetY: 16
    };

    var standLeft = {
        cellWidth: 32,
        cellHeight: 32,
        gridSpace: 1,
        x: 18,
        y: 3,
        offsetX: 16,
        offsetY: 16
    };

    var shootUp = {
        cellWidth: 32,
        cellHeight: 32,
        interval: 150,
        offsetX: 16,
        offsetY: 16,
        gridSpace: 1,
        frames: [{
            x: 15,
            y: 3
        }, {
            x: 15,
            y: 2
        }, {
            x: 15,
            y: 1
        }]
    };

    var shootRight = {
        cellWidth: 32,
        cellHeight: 32,
        interval: 150,
        offsetX: 16,
        offsetY: 16,
        gridSpace: 1,
        frames: [{
            x: 18,
            y: 4
        }, {
            x: 19,
            y: 4
        }, {
            x: 20,
            y: 4
        }]
    };

    var shootDown = {
        cellWidth: 32,
        cellHeight: 32,
        interval: 150,
        offsetX: 16,
        offsetY: 16,
        gridSpace: 1,
        frames: [{
            x: 15,
            y: 4
        }, {
            x: 15,
            y: 5
        }, {
            x: 15,
            y: 6
        }]
    };

    var shootLeft = {
        cellWidth: 32,
        cellHeight: 32,
        interval: 150,
        offsetX: 16,
        offsetY: 16,
        gridSpace: 1,
        frames: [{
            x: 20,
            y: 3
        }, {
            x: 19,
            y: 3
        }, {
            x: 18,
            y: 3
        }]
    };

    var explosion = new AnimatedSprite(IMAGE, {
        cellWidth: 32,
        cellHeight: 32,
        interval: 150,
        offsetX: 16,
        offsetY: 16,
        gridSpace: 1,
        frames: [{
            x: 1,
            y: 1
        }, {
            x: 2,
            y: 1
        }, {
            x: 3,
            y: 1
        }]
    });

    var sup = new AnimatedSprite(IMAGE, up);
    var sright = new AnimatedSprite(IMAGE, right);
    var sdown = new AnimatedSprite(IMAGE, down);
    var sleft = new AnimatedSprite(IMAGE, left);
    var sStandUp = new ImageSprite(IMAGE, standUp);
    var sStandRight = new ImageSprite(IMAGE, standRight);
    var sStandDown = new ImageSprite(IMAGE, standDown);
    var sStandLeft = new ImageSprite(IMAGE, standLeft);
    var sShootUp = new AnimatedSprite(IMAGE, shootUp);
    var sShootRight = new AnimatedSprite(IMAGE, shootRight);
    var sShootDown = new AnimatedSprite(IMAGE, shootDown);
    var sShootLeft = new AnimatedSprite(IMAGE, shootLeft);

    this.sprites = [sup, sright, sdown, sleft, sStandUp, sStandRight, sStandDown, sStandLeft, sShootUp, sShootRight, sShootDown, sShootLeft, explosion];
    sup.playLoop();
    sright.playLoop();
    sdown.playLoop();
    sleft.playLoop();
    this.spriteIndex = TANK_STAND_UP;
    this.collisionManager.objects.push(this);
}

Tank.prototype.update = function() {
    this.x += this.dx;
    this.y += this.dy;
    this.x1 = this.x - this.halfWidth;
    this.x2 = this.x + this.halfWidth;
    this.y1 = this.y - this.halfHeight
    this.y2 = this.y + this.halfHeight
    this.dx = 0;
    this.dy = 0; 

    for (var i in this.projectiles) {
        this.projectiles[i].update();
    }
};

Tank.prototype.render = function(context) {
    for (var i in this.projectiles) {
        this.projectiles[i].render(context);
    }
    if(this.spriteIndex == TANK_EXPLODE){
        this.sprites[this.spriteIndex].playOnce();
    }
    if (this.visible) {
        this.sprites[this.spriteIndex].x = this.x;
        this.sprites[this.spriteIndex].y = this.y;
        this.sprites[this.spriteIndex].render(context);
        context.fillText(this.name, this.x - this.nameOffsetX, this.y - this.nameOffsetY);
        if (this.life > 0) {
            context.fillText(Array(this.life + 1).join("."), this.x - this.lifeOffsetX, this.y - this.lifeOffsetY);
        } else {
            context.fillText("dead", this.x - this.lifeOffsetX, this.y - this.lifeOffsetY);
        }

    }
};

Tank.prototype.moveUp = function() {
    if (this.notShooting && !this.remove) {
        this.spriteIndex = TANK_MOVE_UP;
        this.face = FACE_UP;
        this.dx = 0;
        this.dy = -1;
    }
};

Tank.prototype.moveRight = function() {
    if (this.notShooting  && !this.remove) {
        this.spriteIndex = TANK_MOVE_RIGHT;
        this.face = FACE_RIGHT;
        this.dx = 1;
        this.dy = 0;
    }
};

Tank.prototype.moveDown = function() {
    if (this.notShooting  && !this.remove) {
        this.spriteIndex = TANK_MOVE_DOWN;
        this.face = FACE_DOWN;
        this.dx = 0;
        this.dy = 1;
    }
};

Tank.prototype.moveLeft = function() {
    if (this.notShooting  && !this.remove) {
        this.spriteIndex = TANK_MOVE_LEFT;
        this.face = FACE_LEFT;
        this.dx = -1;
        this.dy = 0;
    }
};

Tank.prototype.stop = function() {
    if (this.notShooting  && !this.remove) {
        this.spriteIndex = this.face + 4;
        this.dx = 0;
        this.dy = 0;
    }
};

Tank.prototype.fire = function() {
    if (this.notShooting && !this.remove) {
        this.notShooting = false;
        this.dx = 0;
        this.dy = 0;
        var proj = new Projectile(this, this.x, this.y, this.collisionManager);
        switch (this.face) {
            case FACE_UP:
                this.spriteIndex = TANK_SHOOT_UP;
                proj.dx = 0;
                proj.dy = -2;
                proj.spriteIndex = 0;
                break;
            case FACE_RIGHT:
                this.spriteIndex = TANK_SHOOT_RIGHT;
                proj.dx = 2;
                proj.dy = 0;
                proj.spriteIndex = 1;
                break;
            case FACE_DOWN:
                this.spriteIndex = TANK_SHOOT_DOWN;
                proj.dx = 0;
                proj.dy = 2;
                proj.spriteIndex = 2;
                break;
            case FACE_LEFT:
                this.spriteIndex = TANK_SHOOT_LEFT;
                proj.dx = -2;
                proj.dy = 0;
                proj.spriteIndex = 3;
                break;
        }
        this.projectiles.push(proj);
        var self = this;
        this.sprites[this.spriteIndex].onPlayEnd = function() {
            self.notShooting = true;
        };
        this.sprites[this.spriteIndex].playOnce();
    }
};

Tank.prototype.onCollide = function(obj) {
    if (obj == null) {
        this.stop();
    }
    if (obj instanceof Tank) {
        this.stop();
    }
    if (obj instanceof Block) {
        this.stop();
    }
    if (obj instanceof Projectile && obj.owner !== this) {
        this.life--;
    }
    if (this.life <= 0) {
        this.spriteIndex = TANK_EXPLODE;
        this.dx = 0;
        this.dy = 0;
        this.remove = true;
        var self = this;
        this.sprites[this.spriteIndex].onPlayEnd = function() {
            self.visible = false;
        };
    }
};
/*----------- end Tank -----------*/
